package com.planet.stockanalysis.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Utils {
	public static String readFile(String path, Charset encoding){
		byte[] encoded;
		try {
			encoded = Files.readAllBytes(Paths.get(path));
			return encoding.decode(ByteBuffer.wrap(encoded)).toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		}
	}

	public static String readString(String url, String charsetName){
		String ret = "";
		try{
			URL u = new URL(url);
			
			URLConnection conn = u.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, charsetName));
			String line = null;
			while((line = br.readLine()) != null){
				ret += line;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return ret;
	}
}

